<?php

require_once("View/View/LoggedInView.php");
require_once("Database/Model/ExerciseDAL.php");
require_once("Database/Model/MuscleGroupDAL.php");
require_once("Exercises/Model/ExerciseModel.php");

class ExersicesView
{
	/**
	*@var ExerciseDAL
    **/	
	private $exerciseDAL;
	/**
	*@var LoggedInView
    **/
	private $loggedInView;
	/**
	*@var ExerciseModel
    **/
	private $exerciseModel;
	/**
	*@var MuscleGroupDAL
    **/
	private $muscleGroupDAL;

	/**
	*@var string get
    **/
	private static $edit = "edit";
	/**
	*@var string get
    **/
	private static $remove = "remove";
	/**
	*@var string get
    **/
	private static $edited = "edited"; 
	/**
	*@var string get
    **/
	private static $added = "added";
	/**
	*@var string get
    **/
	private static $new = "new";
	/**
	*@var string 
    **/
	private static $finishedEdit = "ExerciseView::finishedEdit";
	/**
	*@var int 
    **/	
	private static $idToEdit = "ExerciseView::IdToEdit";
	/**
	*@var int 
    **/
	private static $idToRemove = "ExerciseView::IdToRemove";
	/**
	*@var int post
    **/
	private static $createdExerciseID = "ExerciseView::createdExerciseID";
	/**
	*@var string post
    **/
	private static $newMuscleGroup = "ExerciseView::newMouscleGroup";
	/**
	*@var string post
    **/
	private static $newExerciseName = "ExerciseView::newExerciseName";
	/**
	*@var string post
    **/
	private static $newExerciseDescription = "ExerciseView::newExerciseDescription";
	/**
	*@var string post
    **/
	private static $newYoutubeLink = "ExerciseView::newYoutubeLink";

	/**
	*@var int 
    **/
	private static $editID = "ExerciseView::editID";

	/**
	*@var string css ID 
    **/
	private static $youtubeLinkTextField = "YoutubeLinkTextField";
	/**
	*@var string css ID 
    **/
	private static $nameTextField = "NameTextField"; 
	/**
	*@var string css ID 
    **/
	private static $descriptionTextBox = "DescriptionTextBox";
	/**
	*@var string css ID 
    **/
	private static $formSubmitButton = "FormSubmitButton";

	/**
	*@var array() strings
    **/
	private $muscleGroupNames = array(1 =>"Översida lår",2=> "Undersida lår",3=> "Vader",4=> "Biceps",
									   5=> "Triceps", 6=> "Axlar", 7=> "Bröst",8=> "Mage",9=> "Handleder",
									   10=>"Nedre rygg",11=> "Övre rygg");


	public function __construct()
	{
		$this->exerciseDAL = new ExerciseDAL();
		$this->loggedInView = new LoggedInView();
		$this->exerciseModel = new ExerciseModel();
		$this->muscleGroupDAL = new MuscleGroupDAL();
	}

	/**
	*@return string html
    **/
	public function getExercisesPage()
	{
		$html = $this->getExercisesPageHeader();
		$html .= $this->getExercisesPageBody();
		$html .= $this->getExercisesPageFooter();
		return $html;
	}

	/**
	*@return string html header
    **/
	private function getExercisesPageHeader()
	{
		$header = $this->loggedInView->getLoggedInHeader();
		return $header;
	}

	/**
	*@return string html
    **/
	private function getExercisesPageBody()
	{
		$html = "<div id='ExercisesMenuDiv'>".$this->getMenu();                    
		$html .= "<ul>
					<li>
						<a href='Exercises.php?".self::$new."'>Lägg till ny övning</a>
			      </li>
			      </ul
				</div>";
		$html .= $this->getFormMessage();
		$html .= $this->checkWhatPage();
				  
		return $html;
	}

		/**
	*@return string html
    **/
	private function getMenu()
	{
		$html = $this->loggedInView->getLoggedInMenu();
	    return $html;
	}

	/**
	*@return string html
    **/
	private function getExercisesPageFooter()
	{
		$footer = $this->loggedInView->getFooter();
		return $footer;
	}



	/**
	*@return string html
    **/
	private function checkWhatPage()   
	{
		if($this->checkIfCreateNewExercise() === true || $this->checkIfAddNewExercise())
	    {
	    	$html = $this->createNewExerciseForm();
	    }
	    elseif($this->editExerciseOnID() === true || $this->finishedEdit())
		{
			$html = $this->createNewExerciseForm(true);
		}	
		else
		{	
			$html = $this->getExercises($this->exerciseModel->getSuccesMessage(), 
									    $this->exerciseDAL->getExerciseList(),
									    true);
		}
		return $html;
	}



	/**
	*@param bool
	*@return string html
    **/
	private function createNewExerciseForm($edit = false)
	{		
		$eName =""; $description = ""; $youtubeLink =""; $buttonValue ="";
		$buttonValue = "Skapa";   $action = self::$added;
		
		$exerciseId = $this->getEditId();
		
		if($edit)
		{
			$exercise = $this->exerciseDAL->getExerciseList($exerciseId);
			$eName = $exercise->getExerciseName();
			$description = $exercise->getExerciseDescription();
			$youtubeLink = $exercise->getYoutubeLink();
			$buttonValue = "Redigera";
			$action = self::$edited;
		}
		else
		{
			$eName = $this->getNewExerciseName();
			$description = $this->getNewExerciseDescription(); 
			$youtubeLink = $this->getNewYoutubeLink(); 
		}
	
		return  $this->ExerciseForm($eName, $description, $youtubeLink, $buttonValue, $action);
	}


	/**
	*@param string
	*@param string
	*@param string
	*@param string
	*@param string
	*@return string html
    **/
	private function ExerciseForm($eName, $description, $youtubeLink, $buttonValue,  $action)
	{
		$html = "
		<div id='createExerciseFormDiv' >
			<a href='Exercises.php'>tillbaka</a>
			<form action='?$action' method='post' enctype='multipart/form-data'>
				<fieldset> 
					<legend>Lägg till ny övning</legend>
						<label for='mouscleGroup' >Muskelgrupp :</label>";

		$html .= $this->getMuscleGroupScrollDown();

		$html .= "  <label for='".self::$nameTextField."' >Övningsnamn :</label>
					<input type='text' size='40' name='" . self::$newExerciseName . "' id='".self::$nameTextField."' value='$eName' />
						
					<label for='".self::$descriptionTextBox."' >Beskrivning :</label>
					<textarea rows='4' cols='50' name='" . self::$newExerciseDescription . "' id='".self::$descriptionTextBox."'>$description</textarea>

					<label for='". self::$youtubeLinkTextField ."' >Lägg till en youtube länk som visar övningen :</label>
					<input type='text' size='40' name='" . self::$newYoutubeLink . "' id='" . self::$youtubeLinkTextField . "' value='$youtubeLink' />

					<input type='submit' name='' id='".self::$formSubmitButton."' value='$buttonValue' />
				</fieldset>
			</form>
		</div>";  

		return $html;
	}


	/**
	*@return string html
    **/
	private function getMuscleGroupScrollDown()
	{
		$html .= "<div class='musclegroupDropdown'><select name='" . self::$newMuscleGroup . "' id='mouscleGroup'>";
		$arr = $this->muscleGroupDAL->getMuscleGroupList();

		foreach($arr as $value) 
		{
			$id = $value->getMuscleGroupID();
			$name = $value->getMuscleGroup();
			$html .= "<option value='$id'>$name</option>";
		}
		$html .= "</select></div>";

		return $html;
	}
 
	/**
	*@return string html
    **/
	private function getFormMessage()
	{
		$message ="";
		$errormessage = $this->exerciseModel->getErrorMessage();
		$successMessage = $this->exerciseModel->getsuccesMessage();

		if($errormessage != "")
		{
			$message = "<div id='ErrorMessageDiv'>
							<p>$errormessage</p>
						</div> ";
		}
		elseif($successMessage != "")
		{
			$message = "<div id='SuccessMessageDiv'>
							<p>$successMessage</p>
						</div> ";
		}

		return $message;
	}


	/**
	*@return int get/session.
    **/
	private function getEditId()
	{
		$id = $_GET[self::$edit];

		if($id == null)
		{
			$id = $this->exerciseModel->getIdToEdit();
		}
		else
		{
			$this->exerciseModel->setIdToEdit($id);
		}
		return $id;
	}

	/**
	*@return string array
    **/
	private function GetExerciseUnderMGroupArray()
	{
		return array(1 => "<div id='oneExercise'><h2>Översida lår</h2>", 
				     2 => "<div id='oneExercise'><h2>Undersida lår</h2>", 
				     3 => "<div id='oneExercise'><h2>Vader</h3>", 
				     4 => "<div id='oneExercise'><h2>Biceps</h2>", 
				     5 => "<div id='oneExercise'><h2>Triceps</h2>", 
			   	     6 => "<div id='oneExercise'><h2>Axlar</h2>", 
				     7 => "<div id='oneExercise'><h2>Bröst</h2>" , 
				     8 => "<div id='oneExercise'><h2>Mage</h2>", 
				     9 => "<div id='oneExercise'><h2>Handleder</h2>", 
				     10 => "<div id='oneExercise'><h2>Nedre rygg</h2>", 
				     11 => "<div id='oneExercise'><h2>Övre rygg</h2>");
	}


	/**
	*@param string html
	*@param Exercise html
	*@param bool  
	*@return string html
    **/
	public function getExercises($message = null, $exerciseList, $onlyView = false)
	{
		if($exerciseList == null)
	    {
	    	$html .= "<p>Du har ej skapat någon övning ännu</p>";
	    }
	    else
	    {
			$html = "<div id='exercisesViewexercisesDiv'><div id='ExercisesViewMessage'><p>$message</p></div>";

			$displayMuscle = array(1 => false, 2 => false, 3 => false, 4 => false, 5 => false, 6 => false, 7 => false , 8 => false, 9 => false, 10 => false, 11 => false);
			$exercisesUnderMGroups = $this->GetExerciseUnderMGroupArray();

			foreach($exerciseList as $oneExercise)
			{
				$id = $oneExercise->getExerciseID();
		    	$musclegroup = $oneExercise->getMusclegroup();
			    $name = $oneExercise->getExerciseName();
			    $reps = $oneExercise->getReps();
			    $sets = $oneExercise->getSets();
			    $order = $oneExercise->getOrderNumber(); 

				$description = $oneExercise->getExerciseDescription();		
		
				$htmls = $this->getOneExerciseHTML($id, $name, $onlyView, $reps, $sets, $order, $oneExercise, $description);
			
				if($musclegroup = $oneExercise->getMusclegroupID())
				{
					$exercisesUnderMGroups[$oneExercise->getMusclegroupID()] .= $htmls; 
					$displayMuscle[$oneExercise->getMusclegroupID()] = true;
				}
			}
			for($i = 1; $i <= count($displayMuscle); $i++)
			{
				if($displayMuscle[$i] == true)
				{
					$exercisesUnderMGroups[$i] .= "</div>";
					$html .= $exercisesUnderMGroups[$i]; 
				}
			}
	    }

	    $html .= "</div>";
		return $html;
	}

	/**
	*@param int
	*@param string
	*@param bool  
	*@param int array
	*@param int array
	*@param int array
	*@param Exercise
	*@param string
	*@return string html
    **/
	private function getOneExerciseHTML($id, $name, $onlyView, $reps, $sets, $order, $oneExercise, $description)
	{
		$htmls ="<div id='oneExerciseDiv'>";
		if($onlyView)
		{
			$htmls .= "<a href='?".self::$remove."=$id'>Ta bort</a> <a href='?".self::$edit."=$id' id='exerciseAtag'>Redigera</a>";
		}
		$htmls  .= "<h2>$name</h2>";
		
		if($reps != null && $sets != null && $order != null)
		{
			$htmls  .= '
			<p>Reps: '. $reps . '</p>
			<p>Sets: '. $sets . '</p>
			<p>Ordningsnummer: ' . $order . '</p>';
		}
		if( $oneExercise->getYoutubeLink() != "")
		{
			$htmls .='<p>Youtube länk: </p><a href='.$oneExercise->getYoutubeLink().'>Se video</a>';
		}
		else
		{
			$htmls .='<p>Youtube länk: </p><a>Inget videoklipp är länkat för den här övningen.</a>';
		}

		$htmls  .='<h3>Beskrivning</h3><p>'.$description.'</p></div>';
		return $htmls;
	}



	/**
	*@return bool 
    **/
	public function finishedEdit()
	{
		if(isset($_GET[self::$edited]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return int
    **/
	public function getIdToEdit()
	{
		if(isset($_GET[self::$edit]))
		{
			return $_GET[self::$edit];
		}
		return 0;
	}

	/**
	*@return int
    **/
	public function getIdToRemove()
	{
		if(isset($_GET[self::$remove]))
		{
			return $_GET[self::$remove];
		}
		return 0;
	}

	/**
	*@return int
    **/
	public function getcreatedExerciseID()
	{
		if(isset($_GET[self::$createdExerciseID]))
		{
			return $_GET[self::$createdExerciseID];
		}
		return 0;
	}

	/**
	*@return bool 
    **/
	public function removeExerciseOnID()
	{
		if(isset($_GET[self::$remove]) && $_GET[self::$remove] != null)
		{
			return true;
		}
		return false;
	}


	/**
	*@return bool 
    **/
	public function editExerciseOnID()
	{
		if(isset($_GET[self::$edit]) && $_GET[self::$edit] != null)
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool 
    **/
	public function editedExercise()
	{
		if(isset($_GET[self::$edited]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool 
    **/
	public function checkIfCreateNewExercise()
	{
		if(isset($_GET[self::$new]))
		{
			return true;
		}
		return false;
	}

	/**
	*@return bool 
    **/
	public function checkIfAddNewExercise()
	{
		
		if(isset($_GET[self::$added]))
		{
			return true;
		}
		return false;
	}




	/**
	*@return POST int
    **/
	public function getNewMuscleGroupID()
	{
		if(isset($_POST[self::$newMuscleGroup]))
		{
			return $_POST[self::$newMuscleGroup];
		}
		return "";
	}

	/**
	*@return POST string
    **/
	public function getNewExerciseName() 
	{
		if(isset($_POST[self::$newExerciseName]))
		{
			return $_POST[self::$newExerciseName];
		}
		return "";
	}

	/**
	*@return POST string
    **/
	public function getNewExerciseDescription() 
	{
		if(isset($_POST[self::$newExerciseDescription]))
		{
			return $_POST[self::$newExerciseDescription];
		}
		return "";
	}


	/**
	*@return POST string
    **/
	public function getNewYoutubeLink()
	{
		if(isset($_POST[self::$newYoutubeLink]))
		{
			return $_POST[self::$newYoutubeLink];
		}
		return "";
	}
	
 	 

}